package com.ncmmall.application.business.goods;

import cn.hutool.core.util.StrUtil;
import com.ncmmall.domain.business.goods.attribute.GoodsAttribute;
import com.ncmmall.domain.business.goods.attribute.GoodsAttributeRepository;
import com.ncmmall.domain.business.goods.attribute.QGoodsAttribute;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author linxs
 * @Description: 商品规格管理业务逻辑处理
 * @Date: 2019-07-09 18:30
 */
@Service
@Transactional
public class GoodsAttributeApplication {

    @Autowired
    private GoodsAttributeRepository attributeRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 添加规格
     */
    public void create(GoodsAttribute attribute) {
        attribute.setStatus(2);
        attributeRepository.save(attribute);

        List<GoodsAttribute> list = buildGoodsAttributes(attribute.getAttributeData(), attribute);
        attributeRepository.save(list);
    }

    private List<GoodsAttribute> buildGoodsAttributes(String attributeData, GoodsAttribute attribute) {

        List<String> attributes = Splitter.on(";").trimResults().omitEmptyStrings().splitToList(attributeData);
        Set<String> names = new HashSet<>();

        List<GoodsAttribute> list = new ArrayList<>();
        for (String item : attributes) {
            GoodsAttribute attributeItem = new GoodsAttribute();
            String name = item.substring(item.indexOf(":") + 1);
            if (StrUtil.isBlank(name) || names.contains(name)) {
                continue;
            }
            names.add(name);
            attributeItem.setName(name);
            attributeItem.setType(attribute.getType());
            attributeItem.setParent(attribute);
            attributeItem.setMerchant(attribute.getMerchant());
            list.add(attributeItem);
        }
        return list;
    }

    /**
     * 修改规格信息
     */
    public void modify(GoodsAttribute params) {

        GoodsAttribute goodsAttribute = queryChannelService.findOne(QGoodsAttribute.goodsAttribute.id.eq(params.getId()), GoodsAttribute.class);
        goodsAttribute.setName(params.getName());
        goodsAttribute.setNote(params.getNote());
        attributeRepository.save(goodsAttribute);

        goodsAttribute.getAttributes().removeIf(attr -> attr.getId() == 1 || attr.getId() == 2);
        attributeRepository.delete(goodsAttribute.getAttributes());
        List<GoodsAttribute> list = buildGoodsAttributes(params.getAttributeData(), goodsAttribute);
        attributeRepository.save(list);

    }


    /**
     * 删除规格信息
     */
    public void remove(GoodsAttribute params) {

        if (params.getId() == 1 || params.getId() == 2) {
            return;
        }

        params.getAttributes().removeIf(attr -> attr.getId() == 1 || attr.getId() == 2);
        attributeRepository.delete(params.getAttributes());
        params.setParent(null);
        params.setAttributes(null);

        attributeRepository.delete(params);
    }

    /**
     * 启用规格
     */
    public void enable(GoodsAttribute attribute) {
        attribute.setStatus(1);
        attributeRepository.save(attribute);
    }

    /**
     * 禁用规格
     */
    public void disable(GoodsAttribute attribute) {
        attribute.setStatus(2);
        attributeRepository.save(attribute);
    }

    /**
     * 删除规格值
     */
    public void removeAttributeValue(GoodsAttribute attribute) {
        if (attribute.getId() == 1 || attribute.getId() == 2) {
            return;
        }
        attribute.setParent(null);
        attributeRepository.delete(attribute);
    }
}
